﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Basmus.DataStructures.Generic
{
	public static class ITupleExtension
	{
		public static ITuple<TTarget> Select<TSource, TTarget>(this ITuple<TSource> source, Func<TSource, TTarget> elementSelector, Func<IEnumerable<TSource>, IEnumerable<TTarget>> elementsSelector)
		{
			if (source.IsEnumerable)
			{
				return new Tuple<TTarget>(elementsSelector(source.Elements));
			}
			else
			{
				return new Tuple<TTarget>(elementSelector(source.Element));
			}
		}
		public static ITuple<TTarget> Select<TSource, TTarget>(this ITuple<TSource> source, Func<TSource, TTarget> elementSelector)
		{
			if (source.IsEnumerable)
			{
				return new Tuple<TTarget>(source.Elements.Select(elementSelector));
			}
			else
			{
				return new Tuple<TTarget>(elementSelector(source.Element));
			}
		}
		public static ITuple<TElement> Cast<TElement>(this ITuple tuple)
		{
			if (tuple.IsEnumerable)
			{
				return new Tuple<TElement>((IEnumerable<TElement>)tuple.Entity);
			}
			else
			{
				return new Tuple<TElement>((TElement)tuple.Entity);
			}
		}
	}
}
